package MOLE;

import java.util.*;

/** 
 dbadd ��ժҪ˵����
*/
public class SQL_DBOS
{

	private String pre = " INSERT INTO ";
//		   (txt) VALUES (   @txt  )";
	 private String cols = "";
	private String mid = ") VALUES (";
	private String values = "";
	private String end = ")";

	private String uppre = "update ";
	private String upcols = "";
	private String upmid = " where ";
	private ArrayList sqlParas = new ArrayList();
	private String sql;
	public static String connstr = MOLE.SQL.connectionString;
	public int KeyID;

	public SQL_DBOS(String tableName, String connnstr)
	{
		//
		// TODO: �ڴ˴���ӹ��캯���߼�
		//
		String s = tableName;

		pre = pre + s + "(";
		uppre = uppre + s + " set ";
		//connstr=connnstr;

	}

	public final void add(String columename, Object o)
	{
		cols += columename + ",";
		values += "@" + columename + ",";

		upcols += columename + "=@" + columename + ",";
		sqlParas.add(new SqlParameter("@" + columename,o));
	}

	public final String getsql()
	{
		cols = cols.substring(0,cols.length() - 1);
		values = values.substring(0,values.length() - 1);
		String s = pre + cols + mid + values + end;
		return s;

	}
	public final void SetKeyID(String name, int i)
	{


	}

	public final String GetUpdateSql()
	{

		String tmp = upcols.substring(0,upcols.length() - 1);

		String s = uppre + tmp + upmid;
		return s;

	}

	public final void Add()
	{
		SqlConnection conn = null;
		SqlCommand cmd = null;
		try
		{
			 conn = new SqlConnection(connstr);
			conn.Open();
			sql = getsql();
			cmd = new SqlCommand(sql,conn);
			for (int n = 0;n < sqlParas.size();n++)
			{
				cmd.Parameters.Add(sqlParas.get(n));

			}
			cmd.ExecuteNonQuery();
		}
		catch (RuntimeException ee)
		{
			throw new RuntimeException(ee.getMessage());
		}
		finally
		{
			cmd.Parameters.Clear();

			cmd.Dispose();
			conn.Close();
			conn.Dispose();
		}
	}

	public final void Update(String primaryKey, int primaryKeyValue)
	{
		SqlConnection conn = new SqlConnection(connstr);
		conn.Open();
		upmid += primaryKey + "=" + primaryKeyValue;
		sql = GetUpdateSql();
		SqlCommand cmd = new SqlCommand(sql,conn);
		for (int n = 0;n < sqlParas.size();n++)
		{
			cmd.Parameters.Add(sqlParas.get(n));

		}
		cmd.ExecuteNonQuery();
		//cmd.ExecuteNonQuery();
		cmd.Parameters.Clear();

		cmd.Dispose();
		conn.Close();
		conn.Dispose();
		//System.Web.HttpContext.Current.Response.Write(sqlParas[0].GetType().ToString());
	}


	public static DataSet UpdateByDataSet(DataSet ds, String strTblName)
	{
		return UpdateByDataSet(ds, strTblName, MOLE.SQL.connectionString);
		//			MOLE.SQL.c
		//			 
		//			return new DataSet();
	}

	public static DataSet UpdateByDataSet(DataSet ds, String strTblName, String strConnection)
	{
		SqlConnection conn = new SqlConnection(strConnection);
		SqlDataAdapter myAdapter = new SqlDataAdapter();
		SqlCommand myCommand = new SqlCommand("select * from " + strTblName,(SqlConnection) conn);
		myAdapter.SelectCommand = myCommand;
		SqlCommandBuilder myCommandBuilder = new SqlCommandBuilder(myAdapter);
		try

		{

			//lock(this)            //����������(�ֲ�ʽ���)

		{

			myAdapter.Update(ds,"ds");

		}

		}

		catch (RuntimeException err)
		{

			conn.Close();
			//throw new BusinessException(err);
			throw new RuntimeException(err.getMessage());
		}

		return ds; //���ݼ�����״̬�ڸ��º�ᶼ��Ϊ: UnChange,����θ��º�ͻ���Ҫ�÷��ص�ds

	}

}